import { defineStore } from 'pinia';
import {computed, ref} from 'vue';
export const useCounterStore = defineStore('counter', () => {
//   const count = ref(0)
//   const doubleCount = computed(() => count.value * 2)
//   function increment() {
//     count.value++
//   }

//   return { count, doubleCount, increment }

const cartList=ref([])
//加入购物车
const addItem=(payload)=>{
    const index=cartList.value.findIndex(item=>item.id==payload.id);
    if(index>-1){
        cartList.value[index].count++;
    }
    else{
        cartList.value.push({
            ...payload,
            count:1,
            checked:false,
        })
    }
}
//删除购物车
const deleteItem=(id)=>{
    cartList.value=cartList.value.filter(item=>item.id!=id)
}
//购物车总数量
// const totalCount=computed(()=>{
//     let res=0;
//     cartList.value=cartList.value.forEach(item=>{
//         res+=item.count
//     })
//     return res;
// })

//购物车总价格
// const toatlPrice=computed(()=>{
//     let res=0;
//     cartList.value=cartList.value.forEach(item=>{
//         if(item){
//             res+=item.count*item.price
//         }
//     })
//     return res;
// })

return {cartList,addItem,deleteItem}
},{
    persist:true,
})